package com.ljx.sso.service;

import com.ljx.entity.BusinessInfo;
import com.ljx.entity.EmpInfo;
import com.ljx.entity.Permission;
import com.ljx.entity.UserInfo;
import com.ljx.sso.feign.BUsinessFeign;
import com.ljx.sso.feign.EmpInfoFeign;
import com.ljx.sso.feign.PermissionFeign;
import com.ljx.sso.feign.UserFeign;
import com.ljx.sso.vo.MyUser;
import com.ljx.sso.vo.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2023/2/22 20:48
 * @Version 1.0
 */
@Service
public class UserService implements UserDetailsService {

	@Autowired
	private EmpInfoFeign empInfoFeign;

	@Autowired
	private PermissionFeign permissionFeign;

	@Autowired
	private UserFeign userFeign;

	@Autowired
	private BUsinessFeign bUsinessFeign;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		HttpServletRequest request = WebUtils.getRequest();
		String login_type = request.getParameter("login_Type");


		System.out.println(login_type);
		if("USER_PHONE".equals(login_type)){
			UserInfo userInfo = userFeign.getByTel(username);
			if(userInfo!=null){
				EmpInfo empInfo = empInfoFeign.getByName(username);
				List<Permission> permissions = permissionFeign.getPermissionByUserid("1");
				Collection<SimpleGrantedAuthority> authorities = permissions.stream()
						.filter(item->item.getCode()!=null)
						.map(item->new SimpleGrantedAuthority(item.getCode()))
						.collect(Collectors.toList());
				MyUser user = new MyUser(userInfo.getTelephone(),userInfo.getPassword(),authorities,"USER_PHONE");
				user.setLoginType(login_type);
				return user;
			}

		}else if("BUSINESS_PHONE".equals(login_type)){
			System.out.println("login_type = " + login_type);
			BusinessInfo businessInfo = bUsinessFeign.getByTel(username);
			System.out.println(businessInfo);
			if(businessInfo!=null){
				EmpInfo empInfo = empInfoFeign.getByName(username);
				List<Permission> permissions = permissionFeign.getPermissionByUserid("1");
				Collection<SimpleGrantedAuthority> authorities = permissions.stream()
						.filter(item->item.getCode()!=null)
						.map(item->new SimpleGrantedAuthority(item.getCode()))
						.collect(Collectors.toList());
				MyUser user = new MyUser(businessInfo.getTelephone(),businessInfo.getPassword(),authorities,"BUSINESS_PHONE");
				user.setLoginType(login_type);
				return user;
			}
		}else{
			//1.根据用户名查询用户信息
			EmpInfo empInfo = empInfoFeign.getByName(username);
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+username);
			if(empInfo!=null){
				//2.根据当前用户查询该用户具有的权限。
				List<Permission> permissions = permissionFeign.getPermissionByUserid(empInfo.getId());
				Collection<SimpleGrantedAuthority> authorities = permissions.stream()
						.filter(item->item.getCode()!=null)
						.map(item->new SimpleGrantedAuthority(item.getCode()))
						.collect(Collectors.toList());
				MyUser user = new MyUser(empInfo.getEname(),empInfo.getPassword(),authorities);
				return user;
			}
		}

		return null;
	}
}
